package cn.liulu.utils.blog;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 读取文件夹内的md文件 
 * 转义 
 * 并复制到另外一个文件夹内 
 * 
 * 接收两个参数 一个是源文件夹 一个是目标文件夹
 */
public class App  {
    public static void main( String[] args ) {
      // 获取参数
      String srcPath = args[0];
      String desPath = args[1]; 
      // 读取源文件夹内的文件 循环 挨个转义后 复制到 目标文件夹
      File[] fileList = new File(srcPath).listFiles();
      // 创建目标文件夹
      File desFile = new File(desPath);
      if(!desFile.exists()){
        desFile.mkdirs();
      }
      handle(fileList,desFile);
      System.out.println("over");
    }
    
    public static void handle(File[] files,File des){
      for(File file:files){
        if(file.isDirectory()){
          File desFile = new File(des.getAbsolutePath()+File.separator+file.getName());
          if(!desFile.exists()) desFile.mkdirs();
          handle(file.listFiles(),desFile);
        }else{
          // 判断是md文件则读取 不是则直接复制
          String fileName = file.getName();
          if(fileName.endsWith(".md")){
            String content = readMd(file);
            String transContent = trans(content); 
            File desc = new File(des.getAbsolutePath()+File.separator+file.getName());
            try {
              BufferedWriter bw = new BufferedWriter(new FileWriter(desc));
              bw.write(transContent);
              bw.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }else{
            copy(file,new File(des.getAbsolutePath()+File.separator+file.getName()));
          }
        }
      }
    }
    
    public static String trans(String src){
      char[] cs = src.toCharArray();
      StringBuilder sb = new StringBuilder();
      boolean trans = false;
      for(int i=0;i<cs.length;i++){
        if(cs[i]=='$'){  
          sb.append(cs[i]);
          if(trans){
            trans = false;
          }else{
            trans = true;
          } 
          if(cs[i+1]=='$'){
            sb.append(cs[i+1]);
            i++;
          }
        }else{
          if(trans){
            if(cs[i]=='\\'){
              sb.append("\\").append(cs[i]);
            }else if(cs[i]=='_'){
              sb.append("\\").append(cs[i]);
            }else{
              sb.append(cs[i]);
            }
          }else{
            sb.append(cs[i]);
          }
        }
      }
      return sb.toString();
    }
    
    public static void copy(File src,File des){
      try {
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(des);
        
        int len = 1024;
        byte[] buf = new byte[1024];
        while((len = fis.read(buf))!=-1){
          fos.write(buf, 0, len);
        }
        
        fos.close();
        fis.close();
        
      } catch (Exception e) { 
        e.printStackTrace();
      }
      
    }
    
    public static String readMd(File src){
      try {
        BufferedReader br = new BufferedReader(new FileReader(src));
        StringBuilder sb = new StringBuilder();
        while(br.ready()){
          sb.append(br.readLine()+"\n");
        }
        br.close();
        return sb.toString();
      } catch (Exception e) {
        e.printStackTrace();
        return "";
      } 
    }
}















